package com.example.h1nson.realphotography.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.h1nson.realphotography.R;
import com.example.h1nson.realphotography.config.ConfigURL;
import com.example.h1nson.realphotography.config.GetCurrentUser;
import com.example.h1nson.realphotography.util.AsyncImageLoader;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CommentsAdapter extends BaseAdapter
{
    private LayoutInflater inflater;
    private List list;
    private List<ImageView> listView;
    private int presentPosition;
    public CommentsAdapter(Context context, List<Map> list){
        this.list = list;
        this.listView = new ArrayList<ImageView>();
        this.inflater = LayoutInflater.from(context);
        presentPosition=0;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null)
        {

            Map map = (Map)list.get(position);
            String headURLStr = (String)map.get("headURL");
            String nameStr = (String)map.get("name");
            String commentStr = (String)map.get("comment");
            String timeStr = (String)map.get("time");
            convertView = inflater.inflate(R.layout.item_comments, null);
            LinearLayout linearLy = (LinearLayout) convertView.findViewById(R.id.linearLy);
            ImageView type = (ImageView) convertView.findViewById(R.id.type);
            if(nameStr.equals(GetCurrentUser.currentUser.getUserName())){
            }else{
                type.setVisibility(View.INVISIBLE);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.imageView);
            TextView title = (TextView) convertView.findViewById(R.id.title);
            TextView desc = (TextView) convertView.findViewById(R.id.desc);
            // viewHolder.del = (TextView) convertView.findViewById(R.id.del);
            TextView time = (TextView) convertView.findViewById(R.id.time);

            listView.add(imageView);
            setPhoto(headURLStr);
            title.setText(nameStr);
            time.setText(timeStr);
            desc.setText(commentStr);
        }
        return convertView;
    }
    private void setPhoto(String URL){
        AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
        String imageUrl = ConfigURL.headURL+URL;
        Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new AsyncImageLoader.ImageCallback() {
            public void imageLoaded(Drawable imageDrawable, String imageUrl) {
                if (imageDrawable == null) {
                        //imageView.setImageResource(R.drawable.icon);
                        //imageDrawable = getResources().getDrawable(R.drawable.user_image);
                }
                ImageView imageViewPhoto = listView.get(presentPosition);
                imageViewPhoto.setImageDrawable(imageDrawable);
                presentPosition++;
            }
        });
    }
}
